package cn.lxycx.dataterrace.interceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;

import cn.lxycx.dataterrace.util.ServletPro;

import com.jfinal.aop.Interceptor;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;


/**
 * 微信登录拦截器
 * @author Administrator
 *
 */
public class LoginInterceptor extends AbsLoginIntercept implements Interceptor {

	@Override
	protected String getUser(Controller con) {
		
		Record login_user = con.getSessionAttr("user");
		if(login_user!=null){
			return 	login_user.getStr("account");			
		}else{
			HttpServletRequest req = con.getRequest();
			String acc = ServletPro.getCookie(req, "uuu");
			if(StringUtils.isNotBlank(acc)){
				login_user = Db.findFirst("select * from data_statement_account where account=? and status=1",acc);
				if(login_user!=null){
					con.setSessionAttr("user", login_user);
					return acc;
				}
				//ServletPro.saveCookie(con.getResponse(), k, v)
			}
			return null;
		}
		//System.out.println("useropenid = "+user_openid);
	}

	@Override
	protected String getRedirectUrl() {
		// TODO Auto-generated method stub
		return "/login";
	}

}
